package com.gsbelarus.gedemin.lib.ui;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

import com.gsbelarus.gedemin.lib.R;

final public class NotifHelper {

    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private Context context;

    public static final int ID_NOTIF_FOREGROUND = 123;
    public static final int ID_NOTIF_ERROR = 321;

    public static final int DEMO = R.string.notification_text_create_demo;
    public static final int WORK = R.string.notification_text_connect;

    private String appName;

    public NotifHelper(Context context, String appName) {
        this.appName = appName;
        mNotifyManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context);
        this.context = context;
    }

    public Notification getStartSyncNotification(int resourceIcon, int mode) {
        mBuilder.setContentTitle(appName)
                .setContentText(context.getString(mode))
                .setSmallIcon(resourceIcon)
                .setOngoing(true)
                .setProgress(0, 0, true)
                .setWhen(System.currentTimeMillis());
        return mBuilder.build();
    }

    public void updateDownloadNotification(int value, int currentBlock, int totalBlock) {
        mBuilder.setProgress(100, value, false)
                .setContentText(context.getString(R.string.notification_text_download) + " " + currentBlock + " " +
                        context.getString(R.string.notification_text_from) + " " + totalBlock);
        mNotifyManager.notify(ID_NOTIF_FOREGROUND, mBuilder.build());
    }

    public void showFinishError(String str) {
        if (str != null) {
            mBuilder.setSmallIcon(R.drawable.error)
                    .setProgress(0, 0, false)
                    .setOngoing(false)
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setContentTitle(appName)
                    .setContentText(str)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .setBigContentTitle(appName)
                            .bigText(str));
            mNotifyManager.notify(ID_NOTIF_ERROR, mBuilder.build());
        }
//        if (str != null) {
//            mBuilder.setContentText(str)
//                    .setSmallIcon(R.drawable.error)
//                    .setProgress(0, 0, false)
//                    .setOngoing(false)
//                    .setAutoCancel(true)
//                    .setWhen(System.currentTimeMillis());
//            mNotifyManager.notify(ID_NOTIF_ERROR, mBuilder.build());
//        }
    }

    public void clearNotification() {
        mNotifyManager.cancel(ID_NOTIF_FOREGROUND);
        mNotifyManager.cancel(ID_NOTIF_ERROR);
    }

    public void clearForegroundNotification() {
        mNotifyManager.cancel(ID_NOTIF_FOREGROUND);
    }
}
